package com.qiangesoft.blog.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.blog.common.mvc.PageQuery;
import com.qiangesoft.blog.common.mvc.PageResultVO;
import com.qiangesoft.blog.common.result.ResponseResult;
import com.qiangesoft.blog.common.utils.IpUtils;
import com.qiangesoft.blog.core.entity.BlogLeaveMessage;
import com.qiangesoft.blog.core.mapper.BlogLeaveMessageMapper;
import com.qiangesoft.blog.core.service.IBlogLeaveMessageService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.qiangesoft.blog.common.result.ResultCode.PARAMS_ILLEGAL;

/**
 * <p>
 * 留言 服务实现类
 * </p>
 *
 * @author qiange
 * @since 2023-06-05
 */
@Service
@RequiredArgsConstructor
public class BlogLeaveMessageServiceImpl extends ServiceImpl<BlogLeaveMessageMapper, BlogLeaveMessage> implements IBlogLeaveMessageService {

    private final HttpServletRequest request;

    @Override
    public PageResultVO<BlogLeaveMessage> listMessage(PageQuery pageQuery, String name) {
        PageResultVO<BlogLeaveMessage> pageResultVO = new PageResultVO<>();
        Integer pageNum = pageQuery.getPageNum();
        Integer pageSize = pageQuery.getPageSize();
        pageResultVO.setPageNum(pageNum);
        pageResultVO.setPageSize(pageSize);

        LambdaQueryWrapper<BlogLeaveMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), BlogLeaveMessage::getNickname, name)
                .orderByDesc(BlogLeaveMessage::getCreateTime);
        Page<BlogLeaveMessage> page = baseMapper.selectPage(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()), queryWrapper);
        pageResultVO.setTotal(page.getTotal());
        pageResultVO.setPages(page.getPages());
        pageResultVO.setResults(page.getRecords());
        return pageResultVO;
    }

    /**
     * 批量通过留言
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult passBatch(List<Long> ids) {
        Assert.notEmpty(ids, PARAMS_ILLEGAL.getDesc());
        baseMapper.passBatch(ids);
        return ResponseResult.success();
    }

    /**
     * 删除留言
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteMessageById(int id) {
        baseMapper.deleteById(id);
        return ResponseResult.success();
    }

    /**
     * 批量删除留言
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteBatch(List<Long> ids) {
        int rows = baseMapper.deleteBatchIds(ids);
        return rows > 0 ? ResponseResult.success() : ResponseResult.error("批量删除留言失败");
    }


    //    -------web端方法开始-------

    /**
     * 留言列表
     *
     * @return
     */
    @Override
    public ResponseResult webMessage() {
        // 查询留言列表
        List<BlogLeaveMessage> messageList = baseMapper.selectList(new LambdaQueryWrapper<BlogLeaveMessage>()
                .select(BlogLeaveMessage::getId, BlogLeaveMessage::getNickname, BlogLeaveMessage::getAvatar,
                        BlogLeaveMessage::getContent, BlogLeaveMessage::getTime));
        return ResponseResult.success(messageList);
    }

    /**
     * 添加留言
     *
     * @param message
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult webAddMessage(BlogLeaveMessage message) {
        // 获取用户ip
        String ipAddress = IpUtils.getIp(request);
        String ipSource = IpUtils.getIp2region(ipAddress);
        message.setIpAddress(ipAddress);
        message.setIpSource(ipSource);
        baseMapper.insert(message);
        return ResponseResult.success("留言成功");
    }
}
